#include "mainwindow.h"
#include "constraintswindow.h"
#include <gtkmm/main.h>
#include <iostream>
#include <sstream>

MainWindow::MainWindow(BaseObjectType* cobject,
					   const Glib::RefPtr<Gnome::Glade::Xml>& ui_xml)
	: Gtk::Window(cobject),
	  first_button_(0),
	  back_button_(0),
	  forward_button_(0),
	  last_button_(0),
	  register_button_(0),
	  constraints_button_(0),
	  quit_button_(0),
	  schedule_number_label_(0),
	  schedule_count_label_(0),
	  schedule_treeview_(0),
	  statusbar_(0)
{
	// connect a handler to the delete-event signal, which is sent when the
	// user closes the window
	signal_delete_event().connect(
		sigc::mem_fun(*this, &MainWindow::on_delete_event));

	// initialize and set up child widgets
	ui_xml->get_widget("first_button", first_button_);
	first_button_->signal_clicked().connect(
		sigc::mem_fun(*this, &MainWindow::on_first));
	ui_xml->get_widget("back_button", back_button_);
	back_button_->signal_clicked().connect(
		sigc::mem_fun(*this, &MainWindow::on_back));
	ui_xml->get_widget("forward_button", forward_button_);
	forward_button_->signal_clicked().connect(
		sigc::mem_fun(*this, &MainWindow::on_forward));
	ui_xml->get_widget("last_button", last_button_);
	last_button_->signal_clicked().connect(
		sigc::mem_fun(*this, &MainWindow::on_last));
	ui_xml->get_widget("register_button", register_button_);
	register_button_->signal_clicked().connect(
		sigc::mem_fun(*this, &MainWindow::on_register));
	ui_xml->get_widget("quit_button", quit_button_);
	ui_xml->get_widget("constraints_button", constraints_button_);
	constraints_button_->signal_clicked().connect(
		sigc::mem_fun(*this, &MainWindow::on_constraints));
	quit_button_->signal_clicked().connect(
		sigc::mem_fun(*this, &MainWindow::on_quit));
	ui_xml->get_widget("schedule_number_label", schedule_number_label_);
	ui_xml->get_widget("schedule_count_label", schedule_count_label_);
	ui_xml->get_widget("schedule_treeview", schedule_treeview_);
	init_schedule_view();		// set up tree view:  columns, data etc.
	ui_xml->get_widget("statusbar", statusbar_);
}

MainWindow::~MainWindow()
{
}

/// Helper method for initializing the schedule view.
void MainWindow::init_schedule_view()
{
	ModelColumns columns;  // columns of treeview
	// to set up treeview's layout, we initialize it with a ListStore object,
	// which in turn is set up using the columns object.  we call this list store
	// object the "tree model", since every tree is different depending on its
	// list store layout.
	Glib::RefPtr<Gtk::ListStore> tree_model(Gtk::ListStore::create(columns));
	schedule_treeview_->set_model(tree_model);

	// and now, fill some arbitrary sample data into the treeview
	Gtk::TreeModel::Row row(*(tree_model->append()));
	row[columns.name] = "SOEN";
	row[columns.number] = 228;
	row[columns.type] = "Lec";
	row = *(tree_model->append());
	row[columns.name] = "SOEN";
	row[columns.number] = 228;
	row[columns.type] = "Lab";
	row = *(tree_model->append());	 
	row[columns.name] = "SOEN";
	row[columns.number] = 229;
	row[columns.type] = "Lec";
	row = *(tree_model->append());	 
	row[columns.name] = "SOEN";
	row[columns.number] = 229;
	row[columns.type] = "Tut";
	schedule_treeview_->append_column("Name", columns.name);
	schedule_treeview_->append_column("Number", columns.number);
	schedule_treeview_->append_column("Type", columns.type);
}

/// Handler for the signal emitted when the user clicks the First button.
void MainWindow::on_first()
{
	std::clog << "The First button was clicked." << std::endl;
}

/// Handler for the signal emitted when the user clicks the Back button.
void MainWindow::on_back()
{
	std::clog << "The Back button was clicked." << std::endl;
}

/// Handler for the signal emitted when the user clicks the Forward button.
void MainWindow::on_forward()
{
	std::clog << "The Forward button was clicked." << std::endl;
}

/// Handler for the signal emitted when the user clicks the Last button.
void MainWindow::on_last()
{
	std::clog << "The Last button was clicked." << std::endl;
}

/// Handler for the signal emitted the user clicks the Register button.
void MainWindow::on_register()
{
	std::clog << "The Register button was clicked." << std::endl;
}

/// Handler for the signal emitted the user clicks the Constraints button.
void MainWindow::on_constraints()
{
	std::clog << "The Constraints button was clicked." << std::endl;
	Glib::RefPtr<Gnome::Glade::Xml> ui_xml =
		Gnome::Glade::Xml::create("constraints_window.glade");
	ConstraintsWindow* constraints_window = 0;
	ui_xml->get_widget_derived("constraints_window", constraints_window);
	int response = constraints_window->run();

	switch( response ) {
	case Gtk::RESPONSE_ACCEPT: {
		std::stringstream ss;
		ss << constraints_window->number_of_courses();
	    std::string message;
		ss >> message;
		message += " courses";
		statusbar_->push(message);
		break;
	}
	default:
		break;
	}
}

/// Handler for the signal emitted when the user clicks the Quit button.
void MainWindow::on_quit()
{
	Gtk::Main::quit();
}

/// Handler for the signal emitted when the user closes the main window.
bool MainWindow::on_delete_event(GdkEventAny*)
{
	on_quit();
	return true;
}
